const app = getApp()
import http from '../../utils/http'
Page({

  data: {
    roleId: -1,
    foodList: [],

    // 下拉选择
    selectSex: ['男生', '女生'],
    selectWish: ['减脂', '增肌', '均衡营养'],
    selectSport: ['轻量级活动', '中等强度活动', '高强度活动'],

    showSportDescription: false,

    // 个人身体数据
    height: undefined,
    weight: undefined,
    sex: -1,
    wish: -1,
    sport: -1,
  },

  onShow: function (options) {
    // 判断是否登录已成功
    if (app.globalData.sessionId) {
      this.setData({
        roleId: app.globalData.roleId
      })
      if (this.data.roleId == 3) {
        this.listAllFood()
      } else {
        if (this.data.roleId != 0) {
          this.listTodayFood()
        }
      }
    } else {
      app.userLoginCallback = () => {
        if (app.globalData.sessionId) {
          this.setData({
            roleId: app.globalData.roleId
          })
          if (this.data.roleId == 3) {
            this.listAllFood()
          } else {
            if (this.data.roleId != 0) {
              this.listTodayFood()
            }
          }
        }
      };
    }
  },

  listAllFood: function () {
    http({
      url: '/food/adminPageOrder/1/20',
      method: "post",
      data: {},
      success: (result) => {
        console.log(result)
        this.setData({
          foodList: result.rows
        })
      }
    })
  },

  listTodayFood() {
    http({
      url: '/food/today',
      success: (result) => {
        console.log(result)
        this.setData({
          foodList: result.rows
        })
      }
    })
  },

  toAddFood() {
    wx.navigateTo({
      url: '/pages/food_inner/add/add'
    })
  },
  toDetail(e) {
    wx.navigateTo({
      url: '/pages/food_inner/detail/detail?id=' + e.currentTarget.dataset.id
    })
  },

  bindOffline() {
    var that = this
    console.log(this.data.height)
    if (this.data.sex == -1) {
      wx.showModal({
        title: '提示',
        content: '性别',
      })
      return
    }
    if (this.data.height == undefined) {
      wx.showModal({
        title: '提示',
        content: '请输入身高',
      })
      return
    }
    if (this.data.weight == undefined) {
      wx.showModal({
        title: '提示',
        content: '请输入体重',
      })
      return
    }
    if (this.data.wish == -1) {
      wx.showModal({
        title: '提示',
        content: '请输入饮食意向',
      })
      return
    }
    if (this.data.sport == -1) {
      wx.showModal({
        title: '提示',
        content: '请输入运动强度',
      })
      return
    }
    http({
      url: '/user/bindOffline',
      method: "put",
      data: {
        sex: that.data.sex,
        height: that.data.height,
        weight: that.data.weight,
        sport: that.data.sport,
        wish: that.data.wish
      },
      success: (result) => {
        console.log(result)
        wx.login({
          success: res => {
            console.log(res);
            if (res.code) {
              wx.request({
                url: app.globalData.domainName + '/wxLogin',
                data: {
                  code: res.code
                },
                method: 'POST',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  wx.hideLoading()
                  if (res.data.success) {
                    app.globalData.sessionId = res.data.data.sessionId;
                    app.globalData.roleId = res.data.data.roleId;
                    wx.switchTab({
                      url: '/pages/order/order',
                    })
                  }
                },
                fail: function () {
                  wx.hideLoading()
                  wx.showModal({
                    title: '提示',
                    content: '服务器连接失败，请检查网络',
                  })
                },
              })
            } else {
              wx.hideLoading()
              wx.showModal({
                title: '提示',
                content: '服务器连接失败，请检查网络',
              })
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          },
          fail: function () {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '服务器连接失败，请检查网络',
            })
          },
        })
      }
    })
  },

      // 用户点击右上角分享
      onShareAppMessage: function () {
        const promise = new Promise(resolve => {
            setTimeout(() => {
                resolve({
                    title: '知食 young',
                    imageUrl: "/static/images/logo.png",
                })
            }, 500)
        })
        return {
            title: '知食 young',
            path: "/pages/order/order",
            imageUrl: "/static/images/logo.png",
            promise
        }
    },

  bindPickerChange: function (e) {
    this.setData({
      wish: e.detail.value
    })
  },
  bindPickerChangeSport: function (e) {
    this.setData({
      sport: e.detail.value
    })
  },
  bindPickerChangeSex: function (e) {
    this.setData({
      sex: e.detail.value
    })
  },
})